#include "end_ring_cp.h"
#include "suborder_relations.hpp"

void
end_ring_from_above ( const ZZ& q , const ZZ& j , const ZZ& v_in , const ZZ& DK , int FB_bound , int flag_sieve )
{
  cout << "coucou end ring " << endl;

  ZZ p , D1 , D2 , u , v;
  vec_long vec_e , vec_p , e , p_list;
  long ee;
  int  FB_size , card_D1 , card_test;

  vec_long vec_e_v;
  vec_ZZ   vec_p_v;

  u = 1;
  v = v_in;

  factor_v ( v , vec_e_v , vec_p_v );

  for ( int i = 0 ; i < vec_e_v.length() ; i++ )
  {
    ee = vec_e_v[i];
    p = vec_p_v[i];

    for ( int k = 0 ; k < ee ; k++ )
    {

      D1 = ((v/p)*(v/p)) * DK;
      D2 = (p*p) * DK;
      vec_e.SetLength(0);
      vec_p.SetLength(0);

      FB_size  = FB_size_from_FB_bound ( D1 , FB_bound );

      if ( flag_sieve )
        card_D1 = find_relation_sieve(D1, D2, v, e, p_list , FB_size);
      else
        card_D1 = find_relation_Drew (D1, D2, v, e, p_list ,FB_size,FB_bound);

      for ( int l = e.length()-1 ; l >= 0; l-- )
      {
        if ( e[l] != 0 )
        {
          append(vec_e , (long)abs(int(e[l])) );
          append(vec_p , p_list[l]);
        }
      }

      card_test = relation_cardinality_end_E (vec_e ,vec_p ,q ,j);

      if ( card_test < card_D1 )
        u *= p;
      else
        v /=p;
    }
  }

  cout << "u = " << u << endl;
}

void
test_func_drew ( int argc , char** argv )
{
  for ( int k = 0 ; k < 10 ; k++ )
  {
    int i;
    int m;

    m = test_drew ( argc, argv , (unsigned long)k);
    //printf ( "m = %d\n" , m );
    //m = 991;
    //for ( i = 0 ; i < m ; i++ ) if ( h[i] ) printf ("%ld^%s%ld ", ell[i], (hs[i]<0?"-":""), h[i]);
  }
}

void 
test_func_mike ( const ZZ& D , int FB_bound  , double Tol , long sieve_r , int P_once)
{
  int FB_size  = FB_size_from_FB_bound ( D , FB_bound );
  vec_long e,p_list;

  ZZ v = to_ZZ(1);


  find_relation_sieve_biasse(D,D,v,e,p_list, FB_size,  Tol , sieve_r , P_once , 1);
}

